<?php
/**
 * DirectorySystem
 *
 * @category	FileSystem
 * @package		dayscript.filesystem
 * @subpackage
 * @author		Noel Marin <nmarin@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses
 * @since		1.0
 * @modifiedby	$LastChangedBy: Noel Marin $
 * @modified	$Date: 2009-08-28 $
 */
namespace dayscript\filesystem;

/**
 * DirectorySystem
 *
 * An object representation of system directory
 *
 * @category	FileSystem
 * @package		dayscript.filesystem
 * @subpackage
 * @author		Noel Marin <nmarin@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\filesystem\FileSystem
 * @since		1.0
 * @modifiedby	$LastChangedBy: Noel Marin $
 * @modified	$Date: 2009-08-28 $
 */
class DirectorySystem extends \dayscript\filesystem\FileSystem {

	/**
	 * 0 or 1 value, 0 = Alphabetical in ascending order, 1 = Alphabetical in descending order
	 *
	 * @var int
	 */
	private $filesOrder;

	/**
	 * Default constructor, It will create an DirectorySystem object
	 *
	 * @param $name DirectorySystem complete path
	 * @param $mode Consists of three octal number components specifying access restrictions to the directory for the owner, the user group in which the owner is in, and to everybody else in this order. Default 0777 complete access
	 * @return unknown_type
	 */
	public function __construct($name, $mode = 0777){
		parent::__construct($name, $mode);
		$this->filesOrder = 0; // Alphabetical in ascending order by default
	}

	/**
	 * Checks whether a DirectorySystem is empty
	 *
	 * @return boolean
	 */
	public function isEmpty(){
		$tmp = $this;
		$handle = $tmp->getHandle();
		if(!isset($handle)){
			\dayscript\filesystem\DirectorySystemManager::open($tmp);
			$handle = $tmp->getHandle();
		}

		$flag = true;
		while(false !== ($file = \dayscript\filesystem\DirectorySystemManager::read($handle))){
			if($file == '.' || $file == '..'){
				continue;
			}

			$flag = false;
		}		
		
		\dayscript\filesystem\DirectorySystemManager::close($tmp);
		
		return $flag;
	}

	/**
	 * Return this DirectorySystem files order
	 *
	 * @return string
	 */
	public function getFilesOrder(){
		return $this->filesOrder;
	}

	/**
	 * Set a DirectorySystem files order
	 *
	 * @param $filesOrder
	 * @return void
	 */
	public function setFilesOrder($filesOrder){
		$this->filesOrder = $filesOrder;
	}
}